/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = mysql_detect.c                                   */
/*  NOTE      =                                                    */
/*  DATE      = 2016/04/12 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include <mysql/mysql_core.h>
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "../../../src/module/mysql/config.h"

extern int mysql_detect(Conntrack *p_st_conn, Apm_packet *p_st_packet);

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

static void case1(void)
{
    int i_ret = 0;
    //Conntrack *p_st_conn = NULL;
    Apm_packet st_packet;

    i_ret = mysql_detect(NULL, &st_packet);
    
    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);
    
    return;
}

static void case2(void)
{
    int i_ret = 0;
    Conntrack st_conn;
    //Apm_packet st_packet;

    i_ret = mysql_detect(&st_conn,NULL);
    
    CU_ASSERT_EQUAL(i_ret,APM_L7_IS_NO);
    
    return;
}

static void case3(void)
{
    int i_ret = 0;
    Conntrack st_conn;
    Apm_packet st_packet;
    
    memset(&st_conn, 0x00, sizeof(Conntrack));
    memset(&st_packet, 0x00, sizeof(Apm_packet));

    st_packet.ui_payload_len = 3;
    st_packet.p_payload = (char *)"\x03\x00\x00\x01\x85\xa6\x03";
    
    i_ret = mysql_detect(&st_conn, &st_packet);
    
    CU_ASSERT_EQUAL(i_ret,APM_L7_IS_NO);
    
    return;
}

static void case4(void)
{
    int i_ret = 0;
    Conntrack st_conn;
    Apm_packet st_packet;
    
    memset(&st_conn, 0x00, sizeof(Conntrack));
    memset(&st_packet, 0x00, sizeof(Apm_packet));

    st_packet.ui_payload_len = 6;
    st_packet.p_payload = (char *)"\x03\x00\x00\x01\x85\xa6\x03";
    
    i_ret = mysql_detect(&st_conn,&st_packet);
    
    CU_ASSERT_EQUAL(i_ret,APM_L7_IS_NO);
    return;
}

static void case5(void)
{
    int i_ret = 0;
    Conntrack st_conn;
    Apm_packet st_packet;
    
    memset(&st_conn, 0x00, sizeof(Conntrack));
    memset(&st_packet, 0x00, sizeof(Apm_packet));

    st_packet.ui_payload_len = 7;
    st_packet.p_payload = (char *)"\x03\x00\x00\x01\x85\xa6\x03";
    
    i_ret = mysql_detect(&st_conn,&st_packet);
    
    CU_ASSERT_EQUAL(i_ret,APM_L7_IS_YES);
    
    return;
}

int add_suite_mysql_detect(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" mysql_detect()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL ||
        CU_add_test(pSuite, "case4", case4) == NULL ||
        CU_add_test(pSuite, "case5", case5) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
